package com.lwq.controller;

import com.lwq.dao.system.MenuMapper;
import com.lwq.annotation.PermissionController;
import com.lwq.annotation.PermissionInterface;
import com.lwq.annotation.RepeatSubmit;
import com.lwq.model.Result;
import com.lwq.model.SysUser;
import com.lwq.service.LoginService;
import com.lwq.service.TokenService;
import com.lwq.service.system.MenuService;
import com.lwq.service.system.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * Description:
 * Author: LiuWenQing
 * Datetime: 2021/6/10
 */
@RestController
@PermissionController(type = "system")
public class LoginController extends BaseController {

    @Autowired
    private LoginService service;
    @Autowired
    private PermissionService permissionService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private MenuService menuService;
    @Autowired
    private MenuMapper menuMapper;
    @Value("${permission.administratorAccounts}")
    private String adminAccount;

    /**
     * 登录方法
     *
     * @param user
     * @return
     */
    @PostMapping("login")
    public Object login(@RequestBody @Valid SysUser user) {
        return this.service.doLogin(user);
    }

    /**
     * 获取验证码
     *
     * @return
     */
    @GetMapping("getCaptcha")
    public Object getCaptcha() {
        return null;
    }

    /**
     * 账号注册
     *
     * @param user
     * @return
     */
    @PostMapping("/register")
    @RepeatSubmit
    public Result register(@RequestBody @Valid SysUser user) {
        service.register(user);
        return Result.ok();
    }

    @PutMapping("changePass")
    @RepeatSubmit
    public Result changePass(@RequestBody @Valid SysUser user) {
        service.changePass(user);
        return Result.ok();
    }

    /**
     * 登录后获取当前用户菜单,登录用户权限
     *
     * @return
     */
    @GetMapping("getPermissions")
    @PermissionInterface(code = "permission.query", name = "查询权限")
    public Map<String, Object> getMenusAndPermissions() {
        Boolean flag = Arrays.asList(adminAccount.split(",")).contains(tokenService.getUserName());
        Map<String, Object> reMap = new HashMap<>();
        SysUser user = new SysUser();
        user.setId(tokenService.getUserId());
        user.setUserName(tokenService.getUserName());
        user.setName(tokenService.getName());
        reMap.put("userInfo", user);
        reMap.put("permissions", flag ? permissionService.getAllPermissions(null) : permissionService.getPermissionsByUser(tokenService.getUserId(), null));
        reMap.put("menus", flag ? menuMapper.selectList(null) : menuService.getMenuByUser(tokenService.getUserId(), null));
        return reMap;
    }


    /**
     * 登出
     */
    @PostMapping("logout")
    @RepeatSubmit
    @PermissionInterface(code = "logout", name = "登出")
    public void logout() {
        service.logout(tokenService.getUserId());
    }
}
